package com.lianlian.util;


import com.lianlian.file.FastDFSFile;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;
import org.springframework.core.io.ClassPathResource;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

/**
 * 文件上传
 * 文件信息获取
 * 文件删除
 * storage信息获取
 * tracker信息获取
 */
public class FastDFSUtil {

    /**
     * 加载Tracker连接信息
     */
    static {
        try {
            //查找classpath下的文件路径
            String filename = new ClassPathResource("fdfs_client.conf").getPath();
            ClientGlobal.init(filename);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 文件上传
     * @param fastDFSFile 上传文件封装
     */
    public static  String[] upload(FastDFSFile fastDFSFile) throws Exception {
        //附加参数
        NameValuePair[] meta_list = new NameValuePair[1];
        meta_list[0] = new NameValuePair("author",fastDFSFile.getAuthor());
        /***
         * 文件上传后的返回值
         * uploadResults[0]:文件上传所存储的组名，例如:group1
         * uploadResults[1]:文件存储路径,例如：M00/00/00/wKjThF0DBzaAP23MAAXz2mMp9oM26.jpeg
         */
        //获取TrackerServer
        TrackerServer trackerServer=getTrackerServer();
        //通过TrackerServer的连接信息可以获取Storage的连接信息,创建StorageClient对象储存Storage的连接信息
        StorageClient storageClient=getStorageClient(trackerServer);
        //通过StorageClient访问Storage,实现文件上传,并且获取文件上传后的存储信息
        /**
         * 通过StorageClient访问Storage,实现文件上传,并获取文件上传后的信息
         * 1.上传文件的字节数组
         * 2.文件的扩展名
         * 3.附加参数 比如拍摄地址等
         */
        String[] uploadResults = null;
        uploadResults =storageClient.upload_file(fastDFSFile.getContent(),fastDFSFile.getExt(),meta_list);
        return uploadResults;
    }
    /**
     * 获取文件信息
     * @param  groupname 文件的组名
     * @param  remoteFileName 文件的储存位置
     *
     */
    public FileInfo getFile(String groupname, String remoteFileName) throws Exception {
        //获取TrackerServer
        TrackerServer trackerServer=getTrackerServer();
        //通过TrackerServer获取Storage信息,创建StorageClient对象储存Storage信息
        StorageClient storageClient=getStorageClient(trackerServer);
        //获取文件信息
        return storageClient.get_file_info(groupname,remoteFileName);
    }
    /**
     * 文件下载
     * @param  groupname 文件的组名
     * @param  remoteFileName 文件的储存位置
     */
    public static InputStream downloadFile(String groupname, String remoteFileName) throws Exception {
        //获取TrackerServer
        TrackerServer trackerServer=getTrackerServer();
        //通过TrackerServer获取Storage信息,创建StorageClient对象储存Storage信息
        StorageClient storageClient=getStorageClient(trackerServer);
        //文件下载
        byte[] buffer=storageClient.download_file(groupname,remoteFileName);
        return new ByteArrayInputStream(buffer);
    }
    /***
     * 文件删除实现
     * @param groupName:组名
     * @param remoteFileName：文件存储完整名
     */
    public static void deleteFile(String groupName,String remoteFileName) throws Exception{
        //获取TrackerServer
        TrackerServer trackerServer=getTrackerServer();
        //通过TrackerServer获取Storage信息,创建StorageClient对象储存Storage信息
        StorageClient storageClient=getStorageClient(trackerServer);
       //删除文件
        storageClient.delete_file(groupName,remoteFileName);
    }
    /**
     * 获取storage信息
     */
    public static StorageServer getStorages() throws Exception{
        //创建一个TrackerClient对象,通过TrackerClient对象访问 TrackerClient
        TrackerClient trackerClient = new TrackerClient();
        //获取TrackerServer
        TrackerServer trackerServer=getTrackerServer();
        //获取StorageServer
        return trackerClient.getStoreStorage(trackerServer);
    }

    /***
     * 根据文件组名和文件存储路径获取Storage服务的IP、端口信息
     * @param groupName :组名
     * @param remoteFileName ：文件存储完整名
     */
    public static ServerInfo[] getServerInfo(String groupName, String remoteFileName) throws Exception{
        //创建一个TrackerClient对象,通过TrackerClient对象访问 TrackerClient
        TrackerClient trackerClient = new TrackerClient();
        //获取TrackerServer
        TrackerServer trackerServer=getTrackerServer();
        //获取Storage端口和IP地址信息
        return trackerClient.getFetchStorages(trackerServer,groupName,remoteFileName);
    }

    /**
     * 获取TrackerInfo
     * @return TrackerInfo
     * @throws Exception
     */
    public static String getTrackerInfo() throws Exception{
        //获取TrackerServer
        TrackerServer trackerServer=getTrackerServer();
        //Tracker的IP,HTTP端口
        String ip=trackerServer.getInetSocketAddress().getHostString();
        int tracker_http_port=ClientGlobal.getG_tracker_http_port();
        String url="http://"+ip+":"+tracker_http_port;
        return url;
    }

    /**
     * 封装 获取TrackerServer方法
     * @return TrackerServer
     * @throws Exception
     */
    public  static TrackerServer getTrackerServer() throws Exception{
        //获取TrackerServer
        TrackerServer trackerServer=getTrackerServer();
        return trackerServer;
    }

    public static StorageClient getStorageClient(TrackerServer trackerServer){
        //通过TrackerServer获取Storage信息,创建StorageClient对象储存Storage信息
        StorageClient storageClient=getStorageClient(trackerServer);
        return storageClient;
    }

    //测试文件下载
    public static void main(String[] args) throws Exception {
        /*//1.文件下载
        InputStream is = downloadFile("group1","M00/00/00/wKjThF8fzoOAKYFhAABOQy-PhT4248.jpg");
        //写入文件
        FileOutputStream os = new FileOutputStream("D:/1.jpeg");
        //定义一个缓冲区
        byte[] buffer = new byte[1024];
        while (is.read(buffer)!=-1){
            os.write(buffer);
        }
        os.flush();
        os.close();
        is.close();*/
        //2.删除方法测试
        //deleteFile("group1","M00/00/00/wKjThF8fzoOAKYFhAABOQy-PhT4248.jpg");
        //3.获取storageServerIP 和端口
        /*StorageServer storageServer =getStorages();
        System.out.println(storageServer.getStorePathIndex());
        System.out.println(storageServer.getInetSocketAddress().getHostString());*/
        //获取StorageIP和端口信息
        /*ServerInfo[] serverInfos=getServerInfo("group1","M00/00/00/wKjThF8fzoOAKYFhAABOQy-PhT4248.jpg");
        for (ServerInfo serverInfo : serverInfos) {
            System.out.println(serverInfo);
        }*/

        System.out.println(getTrackerInfo());
    }
}
